package br.com.hotstar.model.DAO.ORM;


import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
/**
 *
 * @author henrique
 */
public class HibernateUtil {
    
    private static final SessionFactory sessionFactory = builsSessionFactory();
    private static ServiceRegistry servise;
    
	private static SessionFactory builsSessionFactory() {
        try{
            Configuration cfg = new Configuration();
            cfg.configure("META-INF/hibernate.cfg.xml");
            servise = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
            return cfg.buildSessionFactory(servise);
        }catch(HibernateException e){
            System.out.println("nao foi possivel conectar" + e);
            throw new ExceptionInInitializerError(e); 
        }
    }
	
	@Produces
	@RequestScoped
    public static Session getSession(){
    	return sessionFactory.getCurrentSession();
    }

	public static SessionFactory getSessionfactory() {
		return sessionFactory;
	}
	
	
	
    
   
   
    
}
